package com.db.springcloud.controller;

import com.db.springcloud.pojo.User;
import com.db.springcloud.service.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//提供restful服务
@Controller
@RestController
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/user/get/{id}")
    @HystrixCommand(fallbackMethod = "queryByIdHystrix")
    public User queryById(@PathVariable("id") int id){
        User user = userService.queryById(id);
        if(user==null){
            throw new RuntimeException("id不存在，或者信息无法找到");
        }
        return user;
    }

    //备选方法
    public User queryByIdHystrix(@PathVariable("id") int id){
        return new User()
                .setId(id)
                .setName("没有对应的信息,null--@Hystrix");
    }


}
